import 'package:shared_preferences/shared_preferences.dart';

class LoginStore {
  // 设置本地存储登录信息
  bool isLogin = false;

  // 保存令牌token
  setToken(String token) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('token', token);
  }

  // 获取token
  getToken() async {
    final prefs = await SharedPreferences.getInstance();
    final token = prefs.getString('token') ?? '';
    return token;
  }

  // 保存用户id
  setUserId(int id) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setInt('id', id);
  }

  // 获取用户名
  getUserId() async {
    final prefs = await SharedPreferences.getInstance();
    final id = prefs.getInt('id') ?? '';
    return id;
  }

  // 保存用户名
  setRealname(String name) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('name', name);
  }

  // 获取用户名
  getRealname() async {
    final prefs = await SharedPreferences.getInstance();
    final name = prefs.getString('name') ?? '';
    return name;
  }

  // 保存用户名
  setUsername(String username) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('username', username);
  }

  // 获取用户名
  getUsername() async {
    final prefs = await SharedPreferences.getInstance();
    final username = prefs.getString('username') ?? '';
    return username;
  }

  // 保存密码
  setPassword(String pwd) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('password', pwd);
  }

  // 获取密码
  getPassword() async {
    final prefs = await SharedPreferences.getInstance();
    final pwd = prefs.getString('password') ?? '';
    return pwd;
  }

  // 保存性别
  setSex(String sex) async {
    Map sexOption={
      '0':'男',
      '1':'女'
    };
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('sex', sexOption[sex]);
  }

  // 获取性别
  getSex() async {
    final prefs = await SharedPreferences.getInstance();
    final sex = prefs.getString('sex') ?? '';
    return sex;
  }

  // 保存班级
  setGrade(String grade) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('grade', grade);
  }

  // 获取班级
  getGrade() async {
    final prefs = await SharedPreferences.getInstance();
    final grade = prefs.getString('grade') ?? '';
    return grade;
  }

  // 保存岗位
  setRole(String role) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setString('role', role);
  }

  // 获取岗位
  getRole() async {
    final prefs = await SharedPreferences.getInstance();
    final role = prefs.getString('role') ?? '';
    return role;
  }


  // 保存是否管理员
  setAdmin(bool admin) async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool('admin', admin);
  }

  // 获取是否管理员
  getAdmin() async {
    final prefs = await SharedPreferences.getInstance();
    final admin = prefs.getBool('admin') ?? '';
    return admin;
  }

  // 退出登录，清除存储的token等用户信息
  logout() async {
    final prefs = await SharedPreferences.getInstance();
    final result = await prefs.clear();
    return result;
  }
}
